<template>
    <el-container class="banner">
        <el-main class="banner__content">
            <div ref="container" class="table-container">
                <div class="banner__header-main">
                    <el-button size="small" type="primary" @click="openDialog('add')">添加banner</el-button>
                </div>
                <el-table ref="table" :data="datas" style="width: 100%" align="center" header-align="center">
                    <el-table-column label="轮播标题" prop="title" />
                    <el-table-column label="轮播跳转地址">
                        <template slot-scope="scope">
                            <el-link :href="scope.row.linkUrl" target="_blank" type="primary">{{ scope.row.linkUrl
                            }}</el-link>
                        </template>
                    </el-table-column>
                    <el-table-column label="轮播背景图" width="200px">
                        <template slot-scope="scope">
                            <el-image :src="scope.row.bannerUrl" />
                        </template>
                    </el-table-column>
                    <el-table-column label="操作">
                        <template slot-scope="scope">
                            <el-button type="danger" size="small" @click="handleDelete(scope.row)">删除</el-button>
                            <el-button size="small" @click="openDialog('edit', scope.row)">更改</el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
        </el-main>

        <el-dialog :title="dialogText" :visible.sync="dialogVisible" width="400" center :lock-scroll="true"  top="40px"
            :close-on-press-escape="false" :close-on-click-modal="false" v-if="dialogVisible">
            <el-form label-width="160px">
                <el-form-item label="轮播标题" required>
                    <el-input v-model="formData.title" placeholder="请输入轮播标题" />
                </el-form-item>
                <el-form-item label="轮播跳转地址" required>
                    <el-input v-model="formData.linkUrl" placeholder="请输入轮播跳转地址" />
                </el-form-item>
                <el-form-item label="轮播背景图上传" required>
                    <upload-picture :url.sync="formData.bannerUrl"  prefix="banner/assets">
                        <div>建议图片长宽比为4:3</div>
                    </upload-picture>
                </el-form-item>
            </el-form>
            <span slot="footer">
                <el-button @click="cancel">取消</el-button>
                <el-button type="primary" @click="handleUpdate">保存</el-button>
            </span>
        </el-dialog>
    </el-container>
</template>

<script>
import uploadPicture from "../../components/uploadPicture/index.vue"
export default {
    name: "bannerList",
    components: {
        uploadPicture,
    },
    data() {
        return {
            datas: [],
            dialogVisible: false,
            dialogVisibleType: "",
            formData: {
                bannerUrl: "",
                linkUrl: "",
                title: "",
            },
        };
    },

    computed: {
        dialogText() {
            if (this.dialogVisibleType === "edit") {
                return "编辑轮播";
            }
            return "添加轮播";
        },
    },

    created() {
        this.handleDatas();
    },
    methods: {
        async handleDatas() {
            const loading = this.$loading();
            try {
                const { data } = await this.$fetch.get("/api/banner/list", {});
                this.datas = data;
                console.log(data);
            } catch (error) {
                console.log(error);
            }
            loading.close();
        },
        refresh() {
            this.handleDatas();
        },

        openDialog(type, row) {
            this.dialogVisibleType = type;
            this.initDialog();
            if (type === "edit") {
                const { bannerUrl, linkUrl, title, id } = row;
                this.formData.bannerUrl = bannerUrl;
                this.formData.linkUrl = linkUrl;
                this.formData.title = title;
                this.formData.id = id;
            }
            this.dialogVisible = true;
        },

        initDialog() {
            this.formData.bannerUrl = "";
            this.formData.linkUrl = "";
            this.formData.title = "";
        },

        cancel() {
            this.dialogVisible = false;
            this.refresh();
        },

        handleDelete(row) {
            const params = {
                id: row.id,
            };
            this.$confirm("确定要删除此轮播？", "", {
                distinguishCancelAndClose: true,
                confirmButtonText: "确认",
                cancelButtonText: "取消",
            }).then(async () => {
                const { code } = await this.$fetch.post("/api/banner/delete", params);
                if (code === 0) {
                    this.$message.success("删除成功");
                }
                this.refresh();
            });
        },

        async handleUpdate() {
            let res;
            const { bannerUrl, title } = this.formData;
            const rules = [
                {
                    case: !title,
                    message: "请输入标题",
                },
                {
                    case: !bannerUrl,
                    message: "请上传图片",
                },
            ]

            const valid = this.$validForm(rules)

            if (!valid) {
                return
            }


            if (this.dialogVisibleType === "edit") {
                res = await this.$fetch.post("/api/banner/update", this.formData);
            } else {
                res = await this.$fetch.post("/api/banner/create", this.formData);
            }

            if (res.code === 0) {
                this.$message.success(
                    this.dialogVisibleType === "edit" ? "修改成功" : "添加成功"
                );
            }

            this.cancel();
        },
    },
};
</script>

<style scoped lang="scss">
.banner {
    display: flex;
    flex-direction: column;
    height: calc(100vh - 50px);

    .banner__header-main {
        height: 40px;
        display: flex;
        align-items: center;
        margin-bottom: 20px;
    }

    .banner__content {
        display: flex;
        flex-direction: column;
        flex: 1;
        overflow: auto;
        padding-bottom: 0;

        .table-container {
            flex: 1;
            overflow: hidden;
            overflow-y: auto;
        }
    }

    .pagination-container {
        display: flex;
        justify-content: center;
        padding: 20px 0;

        .el-pagination {
            padding: 20px 0;
        }
    }
}

::v-deep .preview-image {
    width: 360px;
}
</style>
